package com.jxj.GUI;

import com.jxj.mapper.mapperUser;
import com.jxj.pojo.user;
import com.jxj.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class login extends JFrame {
    private BufferedImage backgroundImage;
    private BufferedImage logoImage;
    private final JTextField usernameField;
    private final JPasswordField passwordField;
    newmain n1 = new newmain();
    private SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
    mapperUser mapperuser = sqlSession.getMapper(mapperUser.class);

    public login() {
        super("智慧东方校园跑管理系统V1.0");
        setUndecorated(true);

        try {
            backgroundImage = ImageIO.read(new File("G:\\JAVA\\campusrunmanagementsystem\\runsystem\\src\\tu\\02.jpg"));
            logoImage = ImageIO.read(new File("G:\\JAVA\\campusrunmanagementsystem\\runsystem\\src\\tu\\logo.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
                int logoWidth = 524;
                int logoHeight = 82;
                int y = 20;
                g.drawImage(logoImage, 20, y, logoWidth, logoHeight, this);

                // 绘制半透明的背景色
                g.setColor(new Color(255, 255, 255, 200));
                // 设置背景色的大小
                int loginBoxWidth = getWidth() / 2;
                int loginBoxHeight = getHeight() / 2;
                int loginBoxX = (getWidth() - loginBoxWidth) / 2;
                int loginBoxY = (getHeight() - loginBoxHeight) / 2;
                g.fillRect(loginBoxX, loginBoxY, loginBoxWidth, loginBoxHeight);
            }
        };

        panel.setLayout(new GridBagLayout());
        JLabel usernameLabel = new JLabel("用户名:");
        JLabel passwordLabel = new JLabel("密码:");
        usernameField = new JTextField(20);
        usernameField.setBorder(BorderFactory.createLineBorder(new Color(0, 90, 141, 255), 2));
        usernameField.setPreferredSize(new Dimension(100, 25));
        passwordField = new JPasswordField(20);
        passwordField.setPreferredSize(new Dimension(100, 25));
        passwordField.setBorder(BorderFactory.createLineBorder(new Color(0, 90, 141, 255), 2));
        JButton loginButton = new JButton("登录");
        n1.changeButton(loginButton);
        JButton registerButton = new JButton("注册账号");
        n1.changeButton(registerButton);
        JButton forgotPasswordButton = new JButton("忘记密码");
        n1.changeButton(forgotPasswordButton);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                System.out.println(username + password);
                if (username.isEmpty() || password.isEmpty()) {
                    JOptionPane.showMessageDialog(
                            null,
                            "用户名或密码不能为空", // 提示消息
                            "提示", // 标题
                            JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                    );
                } else {
                    List<user> userList = mapperuser.getuserList(username);
                    if (userList.isEmpty()) {
                        JOptionPane.showMessageDialog(
                                null,
                                "该账户不存在,请前往注册", // 提示消息
                                "提示", // 标题
                                JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                        );
                    } else {
                        String pword = userList.get(0).getPassword();
                        if (!password.equals(pword)) {
                            JOptionPane.showMessageDialog(
                                    null, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                                    "密码错误请重新输入", // 提示消息
                                    "提示", // 标题
                                    JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                            );
                        } else {
                            setVisible(false);
                            newmain n1 = new newmain();
                            n1.newmain1();
                        }
                    }
                }
            }
        });
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                zhuce z1 = new zhuce();
                z1.zhuceWindow();
            }
        });
        forgotPasswordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String pas = mapperuser.xianshi(username);
                JOptionPane.showMessageDialog(
                        null, // 父组件，如果为 null，则弹窗会显示在屏幕中央
                        "您的密码为:" + pas, // 提示消息
                        "提示", // 标题
                        JOptionPane.INFORMATION_MESSAGE // 提示类型，这里使用信息类型
                );
            }
        });

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(10, 10, 10, 10);
        panel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        panel.add(usernameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        panel.add(passwordField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        panel.add(loginButton, gbc);

        gbc.gridy = 3;
        panel.add(registerButton, gbc);

        gbc.gridy = 4;
        panel.add(forgotPasswordButton, gbc);

        setContentPane(panel);
        setSize(backgroundImage.getWidth(), backgroundImage.getHeight());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setIconImage(new ImageIcon("C:\\Users\\Lenovo\\Desktop\\icon.png").getImage());
    }
}
